<template>
  <view class="container">
    <button @click="open">开启扫描功能</button>
    <button @click="close">关闭扫描功能</button>
	<button @click="startScan">开始扫描</button>
	<button @click="stopScan">停止扫描</button>
    <button @click="startContinuousScan">开始连续扫描</button>
    <button @click="stopContinuousScan">停止连续扫描</button>
    <button @click="toggleScanSound">切换扫描声音</button>
    <button @click="toggleScanVibrator">切换扫描震动</button>
    <button @click="toggleScanEnterKey">切换回车键</button>
    <button @click="setScanPrefix">设置扫描前缀</button>
    <button @click="setScanSuffix">设置扫描后缀</button>
    <button @click="setScanLaserMode">设置扫描激光模式</button>
    <button @click="enableBlockScankey">开启拦截扫描键</button>
    <button @click="disableBlockScankey">关闭拦截扫描键</button>
    <button @click="setScanMode">设置扫描输出模式</button>
    <text>{{ barcodeResult }}</text>
  </view>
</template>

<script>
export default {
  data() {
    return {
      barcodeResult: "",
      scanSoundEnabled: true,
      scanVibratorEnabled: true,
      scanEnterKeyEnabled: true,
      scanPrefix: "PREFIX_",
      scanSuffix: "_SUFFIX",
      scanLaserMode: 1,  // 默认激光模式
      scanMode: 0,       // 默认扫描输出模式
      pluginImpl: null,  // 插件实例
    };
  },
  onLoad() {
    this.pluginImpl = uni.requireNativePlugin('ScanMoudle');
    console.log('onLoad uhfModule', this.uhfModule);
  	
  	plus.globalEvent.addEventListener('onBarCodeReceived', this.handleBarCodeReceived);
  },
  
  onUnload() {
  	plus.globalEvent.removeEventListener('onBarCodeReceived');
  },
  mounted() {
    
  },
  methods: {
	// 事件处理方法
	handleBarCodeReceived(data) {
	  console.log('收到标签数据：', data);  // 添加日志
	  this.lastTag = {
	    barcode: data.barcode 
	  };
	      uni.showToast({
	          title: this.lastTag.barcode,
	          icon: 'none',
	          duration: 2000
	      });
	},
  // 开始扫描
  open() {
    if (this.pluginImpl) {
      this.pluginImpl.open((result) => {
        if (result.code === 'SUCCESS') {
          console.log("Scanner opened:", result);
          this.barcodeResult = "Scanner opened successfully!";
        } else {
          console.error("Failed to open scanner:", result);
          this.barcodeResult = "Failed to open scanner.";
        }
      });
    }
  },

  // 关闭扫描
  close() {
    if (this.pluginImpl) {
      this.pluginImpl.close((result) => {
        if (result.code === 'SUCCESS') {
          console.log("Scanner closed:", result);
          this.barcodeResult = "Scanner closed successfully!";
        } else {
          console.error("Failed to close scanner:", result);
          this.barcodeResult = "Failed to close scanner.";
        }
      });
    } else {
      this.barcodeResult = "插件未加载";
    }
  },
  //开始扫描
startScan() {
    if (this.pluginImpl) {
      this.pluginImpl.openScan((result) => {
        if (result.code === 'SUCCESS') {
          console.log("Scan started:", result);
          this.barcodeResult = "Scan started.";
        } else {
          console.error("Failed to start scan:", result);
          this.barcodeResult = "Failed to start scan.";
        }
      });
    } else {
      this.barcodeResult = "插件未加载";
    }
  },

  // 停止扫描
  stopScan() {
    if (this.pluginImpl) {
      this.pluginImpl.closeScan((result) => {
        if (result.code === 'SUCCESS') {
          console.log("Scan stopped:", result);
          this.barcodeResult = "Scan stopped.";
        } else {
          console.error("Failed to stop scan:", result);
          this.barcodeResult = "Failed to stop scan.";
        }
      });
    } else {
      this.barcodeResult = "插件未加载";
    }
  },
  // 启动连续扫描
  startContinuousScan() {
    if (this.pluginImpl) {
      this.pluginImpl.startContinuousScan((result) => {
        if (result.code === 'SUCCESS') {
          console.log("Continuous scan started:", result);
          this.barcodeResult = "Continuous scan started.";
        } else {
          console.error("Failed to start continuous scan:", result);
          this.barcodeResult = "Failed to start continuous scan.";
        }
      });
    } else {
      this.barcodeResult = "插件未加载";
    }
  },

  // 停止连续扫描
  stopContinuousScan() {
    if (this.pluginImpl) {
      this.pluginImpl.stopContinuousScan((result) => {
        if (result.code === 'SUCCESS') {
          console.log("Continuous scan stopped:", result);
          this.barcodeResult = "Continuous scan stopped.";
        } else {
          console.error("Failed to stop continuous scan:", result);
          this.barcodeResult = "Failed to stop continuous scan.";
        }
      });
    } else {
      this.barcodeResult = "插件未加载";
    }
  },

  // 切换扫描声音
  toggleScanSound() {
    if (this.pluginImpl) {
      const newState = !this.scanSoundEnabled;
      this.pluginImpl.setScanSound(newState, (result) => {
        if (result.code === 'SUCCESS') {
          console.log("Scan sound toggled:", result);
          this.scanSoundEnabled = newState;
          this.barcodeResult = `Scan sound is now ${newState ? "enabled" : "disabled"}.`;
        } else {
          console.error("Failed to toggle scan sound:", result);
          this.barcodeResult = "Failed to toggle scan sound.";
        }
      });
    } else {
      this.barcodeResult = "插件未加载";
    }
  },

  // 切换扫描震动
  toggleScanVibrator() {
    if (this.pluginImpl) {
      const newState = !this.scanVibratorEnabled;
      this.pluginImpl.setScanVibrator(newState, (result) => {
        if (result.code === 'SUCCESS') {
          console.log("Scan vibrator toggled:", result);
          this.scanVibratorEnabled = newState;
          this.barcodeResult = `Scan vibrator is now ${newState ? "enabled" : "disabled"}.`;
        } else {
          console.error("Failed to toggle scan vibrator:", result);
          this.barcodeResult = "Failed to toggle scan vibrator.";
        }
      });
    } else {
      this.barcodeResult = "插件未加载";
    }
  },

  // 切换扫描回车键
  toggleScanEnterKey() {
    if (this.pluginImpl) {
      const newState = !this.scanEnterKeyEnabled;
      this.pluginImpl.setScanEnterKey(newState, (result) => {
        if (result.code === 'SUCCESS') {
          console.log("Scan enter key toggled:", result);
          this.scanEnterKeyEnabled = newState;
          this.barcodeResult = `Scan enter key is now ${newState ? "enabled" : "disabled"}.`;
        } else {
          console.error("Failed to toggle scan enter key:", result);
          this.barcodeResult = "Failed to toggle scan enter key.";
        }
      });
    } else {
      this.barcodeResult = "插件未加载";
    }
  },

  // 设置扫描前缀
  setScanPrefix() {
    if (this.pluginImpl) {
      this.pluginImpl.setScanPrefix(this.scanPrefix, (result) => {
        if (result.code === 'SUCCESS') {
          console.log("Scan prefix set:", result);
          this.barcodeResult = `Scan prefix set to '${this.scanPrefix}'.`;
        } else {
          console.error("Failed to set scan prefix:", result);
          this.barcodeResult = "Failed to set scan prefix.";
        }
      });
    } else {
      this.barcodeResult = "插件未加载";
    }
  },

  // 设置扫描后缀
  setScanSuffix() {
    if (this.pluginImpl) {
      this.pluginImpl.setScanSuffix(this.scanSuffix, (result) => {
        if (result.code === 'SUCCESS') {
          console.log("Scan suffix set:", result);
          this.barcodeResult = `Scan suffix set to '${this.scanSuffix}'.`;
        } else {
          console.error("Failed to set scan suffix:", result);
          this.barcodeResult = "Failed to set scan suffix.";
        }
      });
    } else {
      this.barcodeResult = "插件未加载";
    }
  },

  // 设置扫描激光模式
  setScanLaserMode() {
    if (this.pluginImpl) {
      this.pluginImpl.setScanLaserMode(this.scanLaserMode, (result) => {
        if (result.code === 'SUCCESS') {
          console.log("Scan laser mode set:", result);
          this.barcodeResult = `Scan laser mode set to ${this.scanLaserMode}.`;
        } else {
          console.error("Failed to set scan laser mode:", result);
          this.barcodeResult = "Failed to set scan laser mode.";
        }
      });
    } else {
      this.barcodeResult = "插件未加载";
    }
  },

  // 启用拦截扫描键
  enableBlockScankey() {
    if (this.pluginImpl) {
      this.pluginImpl.enableBlockScankey((result) => {
        if (result.code === 'SUCCESS') {
          console.log("Block scan key enabled:", result);
          this.barcodeResult = "Block scan key enabled.";
        } else {
          console.error("Failed to enable block scan key:", result);
          this.barcodeResult = "Failed to enable block scan key.";
        }
      });
    } else {
      this.barcodeResult = "插件未加载";
    }
  },

  // 禁用拦截扫描键
  disableBlockScankey() {
    if (this.pluginImpl) {
      this.pluginImpl.disableBlockScankey((result) => {
        if (result.code === 'SUCCESS') {
          console.log("Block scan key disabled:", result);
          this.barcodeResult = "Block scan key disabled.";
        } else {
          console.error("Failed to disable block scan key:", result);
          this.barcodeResult = "Failed to disable block scan key.";
        }
      });
    } else {
      this.barcodeResult = "插件未加载";
    }
  },

  // 设置扫描输出模式
  setScanMode() {
    if (this.pluginImpl) {
      this.pluginImpl.setOutScanMode(this.scanMode, (result) => {
        if (result.code === 'SUCCESS') {
          console.log("Scan mode set:", result);
          this.barcodeResult = `Scan mode set to ${this.scanMode}.`;
        } else {
          console.error("Failed to set scan mode:", result);
          this.barcodeResult = "Failed to set scan mode.";
        }
      });
    } else {
      this.barcodeResult = "插件未加载";
    }
  }
}
};
</script>

<style scoped>
.container {
  padding: 20px;
}
button {
  margin: 10px 0;
  padding: 10px;
  background-color: #4CAF50;
  color: white;
  border: none;
  border-radius: 5px;
}
text {
  margin-top: 20px;
  font-size: 16px;
  color: #333;
}
</style>
